/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <folly/init/Init.h>
#include <gflags/gflags.h>

#include "velox/common/file/FileSystems.h"
#include "velox/common/memory/Memory.h"
#include "velox/exec/fuzzer/SpatialJoinFuzzer.h"
#include "velox/functions/prestosql/registration/RegistrationFunctions.h"
#include "velox/parse/TypeResolver.h"
#include "velox/serializers/PrestoSerializer.h"

DEFINE_int64(
    seed,
    0,
    "Initial seed for random number generator used to reproduce previous "
    "results (0 means start with random seed).");

using namespace facebook::velox;

int main(int argc, char** argv) {
  folly::Init init(&argc, &argv);

  // Initialize memory system.
  memory::initializeMemoryManager(memory::MemoryManager::Options{});
  auto pool = memory::memoryManager()->addLeafPool();

  // Register file systems.
  filesystems::registerLocalFileSystem();

  // Register Presto functions.
  functions::prestosql::registerAllScalarFunctions();

  // Register type resolver.
  parse::registerTypeResolver();

  // Register serializers.
  if (!isRegisteredNamedVectorSerde(VectorSerde::Kind::kPresto)) {
    serializer::presto::PrestoVectorSerde::registerNamedVectorSerde();
  }

  // Determine the seed.
  size_t seed = FLAGS_seed == 0 ? std::random_device{}() : FLAGS_seed;
  LOG(INFO) << "Using seed: " << seed;

  // Run the spatial join fuzzer.
  exec::spatialJoinFuzzer(seed);

  return 0;
}
